/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Anders Bo Rasmussen og BT
 */
public class DataBase {

    private Statement state;
    private Connection conn;
    private static DataBase instance;

    private DataBase() {
    }

    public static DataBase getInstance() {
        if (instance == null) {
            instance = new DataBase();
        }
        return instance;
    }

    public boolean login(String username, String password) {
        boolean result = true;
        String name = "ggdb";
        String host = "localhost";
        String port = "3306";
        String db = "jdbc:mysql://" + host + ":" + port + "/" + name;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(db, username, password);
            state = conn.createStatement();
        } catch (ClassNotFoundException | SQLException ex) {
            System.out.println(ex);
            result = false;
        }
        return result;
    }

    public void execute(String sql) throws SQLException {
        state.execute(sql);
    }

    public ResultSet getResult(String sql) throws SQLException, NullPointerException {
        ResultSet rs = state.executeQuery(sql);
        return rs;
    }
    
    public Connection getConnection(){
        return conn;
    }
}
